package cn.itcast.flink.base;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

/**
 * Author itcast
 * Date 2021/7/27 10:07
 * Desc 实现读取本地的数据集
 * 1.env.fromElements(可变参数);
 * 2.env.fromColletion(各种集合);
 * 3.env.generateSequence(开始,结束);
 * 4.env.fromSequence(开始,结束);
 */
public class CollectionSource {
    public static void main(String[] args) throws Exception {
        /**
         * 1.获取本地流执行环境
         * 2.从本地获取数据集
         * 3.打印输出
         * 4.执行环境
         */
        //1.获取本地流执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度
        env.setParallelism(1);
        //2.从本地获取数据集
        DataStreamSource<String> elementSource = env.fromElements("hello world hello flink", "hello hadoop");
        //通过获取集合来获取数据源
        ArrayList<String> words = new ArrayList<>();
        words.add("hadoop");
        words.add("kudu");
        words.add("doris");
        words.add("clickhouse");
        words.add("zeppelin");
        DataStreamSource<String> collectionSource = env.fromCollection(words);
        //wordcount
        //通过生成序列
        DataStreamSource<Long> sequenceSource = env.fromSequence(1, 10);
        //3.打印输出
        elementSource.print("elements ");
        collectionSource.print("collections ");
        sequenceSource.print("sequences ");
        //4.执行环境
        env.execute();
    }
}
